

cap program drop frlu
program define frlu
	args hs
	
	sort h_seq ppos
	list ppos ffpos sex age marstat relhead /*h_respnm */ lineno parent if h_seq==`hs'
	list ppos /* motherID fatherID spouseID */ spouseln pearnval ptotval /*a_parent */ a_exprrp filestat if h_seq==`hs'
	list ppos temp* /* ?_how* */ if h_seq==`hs', ab(30)
end

local y=2006

*/

cd C:\Users\Stuart\Dropbox\InsecurityLit
use CPS_mar`y', clear


// -------------------------------------------------------------------------------------- Homogenization recodes

// Renamings
cap rename hhseq h_seq
cap rename a_spouse spouseln
cap rename pespouse spouseln
cap rename pppos ppos
cap rename a_sex sex
cap rename a_age age
cap rename a_lineno lineno
cap rename pinctot ptotval

// Relationships are more detailed from 1988-onward 
// but we only want to use information that we have 
// available consistently
cap recode a_exprrp (1=1) (2=2) (3/4=3) (5=4) (6/11=5) (12=6) (13/14=7), gen(relhead) 

// Marital status changes slightly
if `y'<1988 qui recode marstat (1=1) (2=2) (3/4=3) (5=4) (6=5) (7=6) (8=7) (*=.), gen(a_maritl)
cap drop marstat
cap rename prmarsta a_maritl
cap rename a_maritl marstat
cap drop temp_hassp
qui gen temp_hassp = inlist(marstat,1,2)

// Student status (for dependent calculation)
cap rename bshlftpt sch_enroll
cap rename a_ftpt sch_enroll
cap rename peschft sch_enroll
cap drop temp_ftst
qui gen temp_ftst = sch_enroll==1


// -------------------------------------------------------------------------------------- Preliminary checks

* qui keep if temp_hassp==1

// Even number of married folks in each HH
cap drop temp_nsps
qui egen temp_nsps=total(temp_hassp), by(h_seq)
cap assert mod(temp_nsps,2)==0
if _rc!=0 {
	di "Uneven number of married persons"
	pause
}

// Who is the "primary earner"
// - We do this to have a more organic notion of who is the HH head 
//   Case 96709 in 2006 is instructive
cap drop temp_pearnamt
cap drop temp_pearner
cap drop temp_npearn
qui egen temp_pearnamt = max(ptotval), by(h_seq)
qui gen temp_pearner = ptotval==temp_pearnamt
qui egen temp_npearn = total(temp_pearner), by(h_seq)


// -------------------------------------------------------------------------------------- Start classifying
cap rename filestat filestat_orig
qui gen filestat=.
#d ;
label define filestat
	1 "Non-filer"
	2 "Single"
	3 "Jointly"
	4 "Dependent filer", replace;
#d cr
label val filestat filestat
qui replace filestat=1 if ptotval==0&marstat>2

// Primary earners - jointly if married, single if not
qui replace filestat=2 if filestat==.&temp_pearner==1&marstat>2&ptotval>0
qui replace filestat=3 if filestat==.&temp_pearner==1&inlist(marstat,1,2)&ptotval>0
preserve // here we use the spouse pointer to distribute the status
	keep if filestat==3
	keep h_seq spouseln filestat
	rename spouseln lineno
	rename filestat temp_sfs
	tempfile spousefilers
	save `spousefilers', replace
restore
merge 1:1 h_seq lineno using `spousefilers'
assert filestat==.|filestat==3 if temp_sfs<.
qui replace filestat = temp_sfs if filestat==.&temp_sfs<.
drop _merge temp_sfs

// Could you be someone's dependent?
cap drop temp_depelig
qui gen temp_depelig = 0
qui replace temp_depelig = 1 if age<19
qui replace temp_depelig = 1 if age<24&temp_ftst==1
// If not then you are for sure a single filer
qui replace filestat=2 if temp_depelig==0&filestat==.


// not if there's no one old enough to claim you
cap drop temp_maxage
qui egen temp_maxage=max(age), by(h_seq)



STOP
cap drop temp_miss
cap drop temp_nmiss
qui gen temp_miss=filestat==.
qui egen temp_nmiss=total(temp_miss), by(h_seq)

// If you are married, you are probably filing jointly
// (unless you live with your parents and have low income)



STOP




// Define the primary family
bys h_seq (ppos): gen temp_N = _N
cap drop temp_prifam
qui gen temp_prifam=inlist(relhead,1,2,3,4)
cap drop temp_nprifam
qui egen temp_nprifam=total(temp_prifam), by(h_seq)
qui count if temp_nprifam==temp_N
di "The primary family classification is definitive for whole household for `=`r(N)'/`=_N'*100' of the population"

// Who is eligible to be a dependent?
// student status 
cap rename bshlftpt sch_enroll
cap rename a_ftpt sch_enroll
cap rename peschft sch_enroll
cap drop temp_ftst
qui gen temp_ftst = sch_enroll==1
// Eligibility flag
cap drop temp_depelig
qui gen temp_depelig=(age<19)
qui replace temp_depelig=1 if age<24&temp_ftst==1
qui replace temp_depelig=0 if inlist(marstat,1,3)

* qui replace temp_depelig=0 if age>17&parent==0

// HH Type 1: 2 spouse, nuclear family
cap drop temp_nuc
qui gen temp_nuc = inlist(relhead,1,3,4)
cap drop temp_anuc
qui egen temp_anuc = min(temp_nuc), by(h_seq)
cap drop temp_mearn
qui egen temp_mearn = max(ptotval), by(h_seq)
cap drop temp_pearn 
qui gen temp_pearn= ptotval==temp_mearn


// Create our own filer status variable
drop temp*
qui gen temp_fstat=.
qui replace temp_fstat=1 if inlist(marstat,1,2,3)

* this case is particularly troublesome:  frlu "96709"



STOP

// Find their children (only for rrp definitives)





// Are relhead 7s always on their own?
cap drop temp_rh7
cap drop temp_arh7
qui gen temp_rh7 = relhead==7
qui egen temp_arh7 = max(temp_rh7), by(h_seq)
qui egen temp_trh7 = total(temp_rh7), by(h_seq)




// TAX UNIT ID WILL BE THE H_SEQ+PPOS OF THE PRIMARY FILER

STOP


